package com.canal.display.controller;

import com.canal.display.entity.canal.ZcolChannel;
import com.canal.display.exception.BuzException;
import com.canal.display.response.Result;
import com.canal.display.response.ResultUtil;
import com.canal.display.service.ItemService;
import com.canal.display.util.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yangbo
 * @date 2020-07-31 9:30
 */
@RestController
public class BaseController {

    @Autowired
    private ItemService itemService;

    /**
     * 渠道号信息
     * @param term
     * @return
     * @throws BuzException
     */
    @GetMapping(value = "/base/items")
    @PreAuthorize("isAuthenticated()")
    public Result itemList(@RequestParam(value = "term",required = false) String term) throws BuzException {
        ZcolChannel zcolChannel = SecurityUtils.currentUser();
        if(zcolChannel == null) {
            throw new  BuzException(500,"未提供用户信息");
        }
        if(StringUtils.isBlank(term)) {
            term = "";
        }
        return ResultUtil.makeSuccess(itemService.itemList(term,zcolChannel.getId()));
    }
}
